package com.zhouyu;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


 
public class FileFilterUtils {


	public static void main(String[] args) throws FileNotFoundException {


		String directoryPath = "E:\\study\\spring-framework-5.3.10";
		String fileName = "Util";
		String fileExtension = ".java";
		String fileContent = "Util";

		List<String> stringList = filterAll(directoryPath, fileName, fileExtension, fileContent);
		for (String file : stringList) {
			if (!file.contains("test"))
			System.out.println("" + file);

		}
		System.out.println("E:\\study\\ruoyi-vue-pro\\yudao-framework\\yudao-spring-boot-starter-web\\target\\classes\\cn\\iocoder\\yudao\\framework\\apilog\\core\\interceptor\\ApiAccessLogInterceptor.class");
		System.out.println("E:\\study\\spring-framework-5.3.10\\tuling\\src\\main\\java\\com\\zhouyu\\FileFilterUtils.java");
	}

    /**
	 通过文件名称查询文件
	 */
	public static List<String> filterByFileName(String directoryPath  ,String fileName ) throws FileNotFoundException {
	return 	filterAll(directoryPath,fileName ,null);
	}


	/**
	 通过文件拓展名称查询文件
	 */
	public static List<String> filterByExtension(String directoryPath  ,String fileExtension ) throws FileNotFoundException {
	return 	filterAll(directoryPath,fileExtension,null);
	}

	/**
	 通过文件中的内容名称查询文件
	 注意这种方式如果换行了可能查询不到，不过不用追求那么仔细
	 */
	public static List<String> filterByContent(String directoryPath  ,String... fileContent ) throws FileNotFoundException {
	return 	filterAll(directoryPath,null,null,fileContent);
	}
	/**
	 通过文件中的内容名称，拓展名称，文件内容 查询文件

	 */
	public static List<String> filterAll(String directoryPath ,String fileName ,String fileExtension  ,String... fileContent ) throws FileNotFoundException {
		if (directoryPath == null || directoryPath.trim().equals("")){
			throw new FileNotFoundException("目录无效");
		}
		if (fileExtension == null || fileExtension.trim().equals("")){
			fileExtension = "";
		}
		if (fileName == null || fileName.trim().equals("")){
			fileName = "";
		}

		List<String> fileNameList = new ArrayList<>();
		List<File> filteredFiles = filterFilesByExtension(new File(directoryPath), fileExtension);
		for (File file : filteredFiles) {
			if (file.getName().toString().contains(fileName)) {
				Scanner scanner = new Scanner(file);
				boolean bool = false;
				breakA: while (scanner.hasNextLine()) {
					String line = scanner.nextLine();

					for (int i = 0; i < fileContent.length; i++) {
						String sss = fileContent[i];
						if (! line.contains(sss)){
							continue breakA;
						}

					}
//					System.out.println(line);
				bool = true;
				}
				if (bool){
					String absolutePath = file.getAbsolutePath();
					fileNameList.add(absolutePath);
					bool = false;
				}

			}

		}
		return fileNameList;
	}

	private static List<File> filterFilesByExtension(File directory, String fileExtension) throws FileNotFoundException {


		List<File> filteredFiles = new ArrayList<>();
		if (directory.exists() && directory.isDirectory()) {
			for (File file : directory.listFiles()) {
				if (file.isDirectory()) {
					filteredFiles.addAll(filterFilesByExtension(file, fileExtension));
				} else if (file.getName().endsWith(fileExtension)) {
					filteredFiles.add(file);
				}
			}
		}
		return filteredFiles;
	}
}
